package com.subfg.common.rabbitmq.util;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class RabbitCallbackConfig implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnsCallback {
    private Logger log = LoggerFactory.getLogger(RabbitCallbackConfig.class);

    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (!ack) {
            log.error("confirm==>发送到broker失败" +
                            "correlationData={}" + "ack={}" + "cause={}",
                    correlationData, ack, cause);
        } else {
            log.info("confirm==>发送到broker成功" +
                            "correlationData={}" + "ack={}" + "cause={}",
                    correlationData, ack, cause);
        }
    }

    @Override
    public void returnedMessage(ReturnedMessage returned) {
        log.info("confirm==>确认是否收到rabbitmq delay mq \n 消息内容如下==>>>{}", JSONObject.toJSONString(returned));
    }
}
